package com.itheima.sh.controller;

import com.itheima.sh.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    DiscoveryClient discoveryClient;




    @GetMapping("{id}")
    public User queryById(@PathVariable("id") String id){

     /*   List<ServiceInstance> serviceInstances = discoveryClient.getInstances("USER-SERVER");
        ServiceInstance serviceInstance = serviceInstances.get(0);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        //通过获取Eureka
        String url = serviceInstance.getUri().toString();
        System.out.println(url+"!!!!!!!!!!");

       // String url = "http://localhost:8081/user/" + id;
        // 调用RestTemplate的getForObject方法，指定url地址和返回值类型
*/

        //使用ribbon负载均衡   USER-SERVER到eureka找到port和端口
        String url = "http://user-server/user/" + id;

        User user = restTemplate.getForObject(url, User.class);
        return user;

    }



}
